package de.upb.aquarium.api.light;

import javax.media.opengl.GL;

import de.upb.aquarium.api.utility.VectorFloat;

/**
 * An abstraction of a OpenGL direction light. This light is fully described
 * by its direction and its three colors. The direction is given in camera
 * space, so you have to position it via a translation first. 
 * @author Jost
 */
public class DirectionalLightSource extends Light {

	private VectorFloat direction;

	/**
	 * Creates  a new DirectionalLightSource with the given three colors.
	 * @param direction The direction of the light.
	 * @param colorAmbient The ambient color.
	 * @param colorDiffuse The diffuse color.
	 * @param colorSpecular The specular color.
	 */
	public DirectionalLightSource(
			VectorFloat direction,
			VectorFloat colorAmbient,
			VectorFloat colorDiffuse,
			VectorFloat colorSpecular)
	{
		// Super constructor call also checks vector sizes.
		super (colorAmbient, colorDiffuse, colorSpecular);
		if (direction.getSize() < 3)
		{
			throw new IllegalArgumentException("The given direction " +
					"has to have at least size 3.");
		}
		
		this.direction = direction;
	}

	@Override
	public void enable(GL gl, int lightIdentifier) {
		// Set the light properties:
		float[] d = new float[4];
		d[0] = -direction.data[0];
		d[1] = -direction.data[1];
		d[2] = -direction.data[2];
		d[3] = 0.0f; // This light is infinitely far away. 
		
		gl.glLightfv(lightIdentifier, GL.GL_POSITION, d, 0);
	}
	
	@Override
	public void disable() {
		// Nothing to do.		
	}
	
	public VectorFloat getDirection() {
		return direction;
	}

	public void setDirection(VectorFloat direction) {
		this.direction = direction;
	}
}
